home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8093 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Leap Years
  5. Date: Fri, 01 Mar 96 12:41:35 GMT
  6. Organization: none
  7. Message-ID: <825684095snz@genesis.demon.co.uk>
  8. References: <8BA8405.02C70020E1.uuout@sourcebbs.com> <4h6ara$mu@netnews1.apci.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4h6ara$mu@netnews1.apci.com> wardmw@apci.com "Martin Ward" writes:
  15.  
  16. >david.mohorn@sourcebbs.com (DAVID MOHORN) typed:
  17. >
  18. >>How do you feature out leap years?  If its evenly divisible by 400 and
  19. >>4?
  20. >
  21. >The following code will do it:
  22. >
  23. >/* Return TRUE if iYear is a leap year, otherwise return FALSE */
  24. >
  25. >int IsLeapYear(int iYear)
  26. >{
  27. >        /* Check for a 4000th year */
  28. >        if ( (iYear % 4000) == 0)
  29. >                return FALSE;
  30.  
  31. Read the FAQ (and the article recently posted by Steve Summit) - there is
  32. *no* 4000 term in the Gregorian calculation of leap years.
  33.  
  34. >
  35. >        /* Check for 400th year */
  36. >        if ( (iYear % 400) == 0)
  37. >                return TRUE;
  38. >
  39. >        /* Check for century */
  40. >        if ( (iYear % 100) == 0 )
  41. >                return FALSE;
  42. >
  43. >        /* C
  44. >        if ( (iYear % 4) == 0 )
  45. >                return TRUE;
  46. >
  47. >        return FALSE;
  48. >}
  49.  
  50. This is also not the best way of writing the calculation since it performs
  51. unnecessary work for most years.
  52.  
  53. int IsLeapYear(int year)
  54. {
  55.         if (year % 4 != 0)
  56.             return FALSE;
  57.  
  58.         if (year % 100 != 0)
  59.             return TRUE;
  60.  
  61.         if (year % 400 != 0)
  62.             return FALSE;
  63.  
  64.         return TRUE;
  65. }
  66.  
  67. This boils down quite neatly to a single expression as the FAQ shows.
  68.  
  69. -- 
  70. -----------------------------------------
  71. Lawrence Kirby | fred@genesis.demon.co.uk
  72. Wilts, England | 70734.126@compuserve.com
  73. -----------------------------------------
  74.